<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="菜谱ID" prop="recipeId">
        <el-input
          v-model="queryParams.recipeId"
          placeholder="请输入菜谱ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮区 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['recipes:steps:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['recipes:steps:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 步骤列表表格 -->
    <el-table v-loading="loading" :data="stepsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="菜谱ID" align="center" prop="recipeId" />
      <el-table-column label="步骤顺序" align="center" prop="stepOrder" />
      <el-table-column label="步骤内容" align="center" prop="content">
        <!-- 内容过长时显示省略号 -->
        <template slot-scope="scope">
          <span v-if="scope.row.content && scope.row.content.length > 30">
            {{ scope.row.content.substring(0, 30) }}...
          </span>
          <span v-else>
            {{ scope.row.content || '无内容' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="步骤图片" align="center" prop="coverImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverImg" :width="50" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['recipes:steps:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />



  <!-- 步骤详情查看对话框 -->
  <el-dialog
    :title="`步骤 ${viewForm.stepOrder || ''} 详情`"
    :visible.sync="viewOpen"
    width="800px"
    append-to-body
  >
    <!-- 步骤内容 -->
    <div class="text-viewer">
      <div v-html="viewForm.content" class="content-display"></div>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button @click="viewOpen = false">关 闭</el-button>
    </div>
  </el-dialog>

  </div>
</template>

<script>
import { listSteps, delSteps } from "@/api/recipes/steps"

export default {
  name: "Steps",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中的ID数组
      ids: [],
      // 非多个选择禁用（删除按钮）
      multiple: true,
      // 是否显示搜索条件
      showSearch: true,
      // 总记录数
      total: 0,
      // 步骤列表数据
      stepsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recipeId: null
      },
      // 查看对话框显示状态
      viewOpen: false,
      // 查看表单数据
      viewForm: {
        stepOrder: null,
        content: null,
        coverImg: null
      }
    }
  },
  created() {
    // 页面初始化时加载数据
    this.getList()
  },
  methods: {
    /**
     * 查询步骤列表
     */
    getList() {
      this.loading = true
      listSteps(this.queryParams).then(response => {
        this.stepsList = response.rows || []
        this.total = response.total || 0
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },

    /**
     * 搜索按钮操作
     */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    /**
     * 重置按钮操作
     */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        recipeId: null
      }
      this.handleQuery()
    },

    /**
     * 多选框选中事件
     */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },



    /**
     * 查看步骤详情
     */
    handleView(row) {
      this.viewForm = {
        stepOrder: row.stepOrder,
        content: row.content || '无步骤内容',
        coverImg: row.coverImg
      }
      this.viewOpen = true
    },

    /**
     * 删除步骤
     */
    handleDelete(row) {
      const ids = row ? [row.id] : this.ids
      if (!ids || ids.length === 0) {
        this.$modal.msgWarning("请选择需要删除的步骤")
        return
      }

      this.$modal.confirm(`是否确认删除步骤编号为"${ids.join(',')}"的数据项？`).then(() => {
        return delSteps(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },

    /**
     * 导出步骤数据
     */
    handleExport() {
      this.download('recipes/steps/export', {
        ...this.queryParams
      }, `steps_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped>
/* 步骤详情查看样式 */
.text-viewer {
  min-height: 300px;
  line-height: 1.8;
  font-size: 14px;
  border: 1px solid #e6e6e6;
  border-radius: 4px;
  padding: 16px;
  overflow-y: auto;
  background-color: #fff;
}

.content-display {
  white-space: pre-wrap; /* 保留换行和空格 */
  word-break: break-word; /* 长单词自动换行 */
}

/* 图片预览样式优化 */
::v-deep .el-image {
  transition: all 0.3s ease;
}

::v-deep .el-image:hover {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

/* 操作列样式 */
.small-padding .el-button--text {
  padding: 0 5px;
}

.fixed-width {
  width: 140px;
}
</style>
